/*
* 文 件 名:  ApplicationDAOImpl.java
* 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2012,  All rights reserved
* 描    述:  应用系统操作
* 修 改 人:  aganzhoul
* 修改时间:  2012-9-12
* 跟踪单号:  <跟踪单号>
* 修改单号:  <修改单号>
* 修改内容:  <修改内容>
*/

package com.tydic.simple.dao.user.impl;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.tydic.framework.dao.AbstractIbatisDAO;
import com.tydic.framework.dataobject.ResultDTO;
import com.tydic.framework.exception.DAOException;
import com.tydic.simple.dao.user.IApplicationDAO;
import com.tydic.simple.dto.user.AppDTO;
import com.tydic.simple.model.user.Application;

/**
* 应用系统数据库操作接口实现
* 
* @author  aganzhoul
* @version  [1.0, 2012-9-12]
* @since  [权限管理/1.0]
*/

public class ApplicationDAOImpl extends AbstractIbatisDAO implements IApplicationDAO {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationDAOImpl.class);
    
    /** {@inheritDoc} */
    @Override
    public Long saveApplication(Application application)
        throws DAOException {
        try {
            sqlMapClient.insert("user.INSERT_APPLICATION", application);
        }
        catch (SQLException e) {
            LOGGER.error("saveApplication exception", e);
            throw new DAOException("saveApplication exception", e);
        }
        return application.getAppId();
    }
    
    /** {@inheritDoc} */
    @Override
    public int deleteAppByCode(String appCode)
        throws DAOException {
        try {
            return sqlMapClient.delete("user.DELETE_APP_BY_CODE", appCode);
        }
        catch (SQLException e) {
            LOGGER.error("deleteAppByCode exception", e);
            throw new DAOException("deleteAppByCode exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @Override
    public Application findAppByCode(String appCode)
        throws DAOException {
        try {
            return (Application)sqlMapClient.queryForObject("user.FIND_APP_BY_CODE", appCode);
        }
        catch (SQLException e) {
            LOGGER.error("findAppByCode exception", e);
            throw new DAOException("findAppByCode exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    public ResultDTO<Application> findApplication(AppDTO appDTO)
        throws DAOException {
        ResultDTO<Application> result = new ResultDTO<Application>();
        try {
            result.setResult(sqlMapClient.queryForList("user.FIND_APPLICATION", appDTO));
            BeanUtils.copyProperties(appDTO.getPagination(), result.getPagination());
            result.getPagination().setTotal((Integer)sqlMapClient.queryForObject("user.FIND_APP_COUNT", appDTO));
        }
        catch (SQLException e) {
            LOGGER.error("findApplication exception", e);
            throw new DAOException("findApplication exception", e);
        }
        return result;
    }
    
}
